package com.clw.weblogin.service.impl;

import com.clw.weblogin.common.entity.ResultEntity;
import com.clw.weblogin.dao.JieKuanLiuShuiMapper;
import com.clw.weblogin.entity.JieKuan;
import com.clw.weblogin.entity.JieKuanLiuShui;
import com.clw.weblogin.entity.JieKuanLiuShuiExample;
import com.clw.weblogin.service.JieKuanLiuShuiService;
import com.clw.weblogin.service.JieKuanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class JieKuanLiuShuiServiceImpl implements JieKuanLiuShuiService {
    @Autowired
    JieKuanLiuShuiMapper mapper;
    @Autowired
    JieKuanService jieKuanService;

    public ResultEntity insert(JieKuanLiuShui bean, String nowdate) {
        ResultEntity entity = new ResultEntity();
        JieKuan jieKuan = new JieKuan();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        double benjin = bean.getCunkuanmoney();
        if (bean.getCaozuoleixing().contains("抽本")) {
            bean.setCunkuanmoney(0.00);
        } else {
        }
        int i = mapper.insert(bean);
        if (bean.getType() != 0) { //流水表type !=0  表示数据为非修改数据
            if (i == 1) {
                //刷新借款表数据
                try {
                    //更新结算时间
                    if (bean.getJiesuandate().getTime() == sdf.parse(nowdate).getTime()) {
                        jieKuan.setEnddate(sdf.parse(nowdate));
                        jieKuan.setJiesuandate(jieKuanService.yujidate(bean.getJiesuandate(), bean.getJiesuanzhouqi()));
                    } else {
                        jieKuan.setEnddate(sdf.parse(nowdate));
                        jieKuan.setJiesuandate(jieKuanService.yujidate(sdf.parse(nowdate), bean.getJiesuanzhouqi()));
                    }
                    jieKuan.setId(bean.getJiekuanid());
                    jieKuan.setType(0);
                    jieKuan.setCunkuanmoney(bean.getCunkuanmoney());
                    if (bean.getCaozuoleixing().contains("抽本")) {
                        jieKuan.setCunkuanmoney(0.00);
                        jieKuan.setType(1);
                        jieKuan.setEnddate(sdf.parse(nowdate));
                        jieKuan.setYujilixi(0.00);
                        int aas = jieKuanService.updateByid(jieKuan);
                        if (aas == 1) {
                            entity.setState(200);
                            entity.setMessage("结算成功,本金：" + benjin + " 利息：" + bean.getLixi());
                        }
                    } else {
                        //预计利息
                        double cunkuanmoney = bean.getCunkuanmoney();
                        int jiesuanzhouqi = bean.getJiesuanzhouqi();
                        double lilv = bean.getLilv();
//                        Calendar calendar = Calendar.getInstance();
//                        String nowda = sdf.format(new Date());
//                        calendar.setTime(sdf.parse(nowda));
//                        long begin = calendar.getTimeInMillis();
//                        calendar.setTime(bean.getJiesuandate());
//                        long end = calendar.getTimeInMillis();
//                        long days = (end - begin) / (1000 * 3600 * 24);
                        if (jiesuanzhouqi == 30) {
                            int value = (int) ((cunkuanmoney * lilv / 30 * 90) * 100);
                            double monry = value / 100;
                            jieKuan.setYujilixi(monry);
                        } else if (jiesuanzhouqi == 360) {
                            int value = (int) ((cunkuanmoney * lilv / 360 * 360) * 100);
                            double monry = value / 100;
                            jieKuan.setYujilixi(monry);
                        }
                        if (jieKuan.getYujilixi() < 0) {
                            jieKuan.setYujilixi(0.00);
                        }
                        int aas = jieKuanService.updateByid(jieKuan);
                        if (aas == 1) {
                            entity.setState(200);
                            double zong = cunkuanmoney + bean.getLixi();
                            entity.setMessage("结算成功,利息：" + bean.getLixi());
                        }
                    }
                } catch (Exception e) {
                    entity.setState(100);
                    entity.setMessage("结算失败,更新借款表失败");
                }
            } else {
                entity.setState(100);
                entity.setMessage("结算失败");
            }
        }
        return entity;
    }

    public ResultEntity selectByExample(JieKuanLiuShui entity, int page, int limit) {
        ResultEntity resultEntity = new ResultEntity();
        JieKuanLiuShuiExample example = new JieKuanLiuShuiExample();
        JieKuanLiuShuiExample.Criteria criteria = example.createCriteria();
        Calendar calendar = Calendar.getInstance();
        criteria.andDepartmentidEqualTo(entity.getDepartmentid());
        criteria.andJiekuanidEqualTo(entity.getJiekuanid());
        example.setStart((page - 1) * limit);
        example.setLimit(limit);
        List<JieKuanLiuShui> list = mapper.selectByExample(example);
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getCunkuandate() != null) {
                    calendar.setTime(list.get(i).getCunkuandate());
                    String yearStr = calendar.get(Calendar.YEAR) + "";//获取年份
                    int month = calendar.get(Calendar.MONTH) + 1;//获取月份
                    String monthStr = month < 10 ? "0" + month : month + "";
                    int day = calendar.get(Calendar.DATE);//获取日
                    String dayStr = day < 10 ? "0" + day : day + "";
                    list.get(i).setStrcunkuandate(yearStr + "/" + monthStr + "/" + dayStr);
                }
                if (list.get(i).getJiesuandate() != null) {
                    calendar.setTime(list.get(i).getJiesuandate());
                    String yearStr = calendar.get(Calendar.YEAR) + "";//获取年份
                    int month = calendar.get(Calendar.MONTH) + 1;//获取月份
                    String monthStr = month < 10 ? "0" + month : month + "";
                    int day = calendar.get(Calendar.DATE);//获取日
                    String dayStr = day < 10 ? "0" + day : day + "";
                    list.get(i).setStrjiesuandate(yearStr + "/" + monthStr + "/" + dayStr);
                }
                if (list.get(i).getCaozuodate() != null) {
                    calendar.setTime(list.get(i).getCaozuodate());
                    String yearStr = calendar.get(Calendar.YEAR) + "";//获取年份
                    int month = calendar.get(Calendar.MONTH) + 1;//获取月份
                    String monthStr = month < 10 ? "0" + month : month + "";
                    int day = calendar.get(Calendar.DATE);//获取日
                    String dayStr = day < 10 ? "0" + day : day + "";
                    int hour = calendar.get(Calendar.HOUR_OF_DAY);
                    String hourstr = hour < 10 ? "0" + hour : hour + "";
                    int min = calendar.get(Calendar.MINUTE);
                    String minstr = min < 10 ? "0" + min : min + "";
                    int ss = calendar.get(Calendar.SECOND);
                    String ssstr = ss < 10 ? "0" + ss : ss + "";
                    list.get(i).setStrcaozuodate(yearStr + "/" + monthStr + "/" + dayStr + " " + hourstr + ":" + minstr + ":" + ssstr);
                }
            }
            resultEntity.setState(200);
            resultEntity.setMessage("成功");
            resultEntity.setData(list);
            Long count = mapper.countByExample(example);
            resultEntity.setCount(count.intValue());
        } else {
            resultEntity.setState(100);
            resultEntity.setMessage("无数据");
        }
        return resultEntity;
    }
}
